package com.weishu.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.weishu.domain.TCwPayOrder;
import com.weishu.service.ITCwPayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("pay")
public class PayController {

    @Autowired
    private ITCwPayOrderService payOrderService;

    @Autowired
    private AlipayClient alipayClient;

    @RequestMapping("payone")
    public String payone() throws AlipayApiException {
        TCwPayOrder tCwPayOrder = new TCwPayOrder();
        tCwPayOrder.setOrderNo(IdUtil.getSnowflakeNextIdStr());
        BigDecimal decimal = new BigDecimal(RandomUtil.randomInt(1, 50));
        tCwPayOrder.setAmount(decimal);
        tCwPayOrder.setCreateTime(new Date());
        tCwPayOrder.setUpdateTime(new Date());
        tCwPayOrder.setDelFlg(0);
        tCwPayOrder.setProductSubject("充值");

        tCwPayOrder.setBeginTime(new Date());
        tCwPayOrder.setProductBody("充值");
        Long id = new Long(0);
        tCwPayOrder.setCreateUserid(id);
        tCwPayOrder.setUpdateUserid(id);
        Long type = new Long(1);
        tCwPayOrder.setOperationType(type);
        tCwPayOrder.setUserId(id);
        tCwPayOrder.setUserType(type);

        int i = payOrderService.insertTCwPayOrder(tCwPayOrder);
        if (i > 0)
            System.out.println("创建payorder成功");
        else
            System.out.println("创建payorder失败");
        System.out.println(tCwPayOrder);

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setTotalAmount(tCwPayOrder.getAmount()+"");
        model.setOutTradeNo(tCwPayOrder.getOrderNo());
        model.setSubject(tCwPayOrder.getProductSubject());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        //request.setReturnUrl("http://localhost:80/money/pay/back");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        String body = response.getBody();
        System.out.println(body);
        if (response.isSuccess()) {
            System.out.println("调用成功");
            tCwPayOrder.setPayAmount(tCwPayOrder.getAmount());
            payOrderService.updateTCwPayOrder(tCwPayOrder);
        } else {
            System.out.println("调用失败");
        }
        return body;
    }

    @RequestMapping("back")
    public void back(String out_trade_no){
        System.out.println(out_trade_no+"支付完毕");
    }

}
